//
//  NSString+ToolExtend.m
//  TTBaseProject
//
//  Created by shny588 on 16/7/13.
//  Copyright © 2016年 tt. All rights reserved.
//

#import "NSString+ToolExtend.h"

@implementation NSString (ToolExtend)

//字符串是否为空
- (BOOL)isNil {
    if ([self isNotNil]) {
        return NO;
    }else{
        return YES;
    }
}

- (BOOL)isNotNil {
    if (!self) {
        return NO;
    }
    if (![self isKindOfClass:[NSString class]]) {
        return NO;
    }
    
    if (![[self removeWhiteSpaces] isEqualToString:@""]&&
        ![[self removeWhiteSpaces] isEqualToString:@"(null)"]&&
        ![[self removeWhiteSpaces] isEqualToString:@"<null>"]) {
        return YES;
    }
    
    return NO;
}

//格式化处理电话号码
- (NSString *)telephoneWithReformat {
    if (!self) {
        return nil;
    }
    NSString *aString = self;
    
    if ([self containsString:@"+86" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@"+86" withString:@""];
    }
    if ([self containsString:@"-" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    if ([self containsString:@"\\" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    }
    //英文括号
    if ([self containsString:@"(" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@"(" withString:@""];
    }
    if ([self containsString:@")" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@")" withString:@""];
    }
    //中文括号
    if ([self containsString:@"（" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@"（" withString:@""];
    }
    if ([self containsString:@"）" withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@"）" withString:@""];
    }
    //中文空格
    if ([self containsString:@" " withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    //英文空格
    if ([self containsString:@" " withAllString:aString]) {
        aString = [aString stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    return aString;
}

//计算 Size
- (CGSize)getStringSizeForFont:(UIFont*)aFont maxSize:(CGSize)maxSize {
    CGFloat maxWidth = maxSize.width;
    CGFloat maxHeight = maxSize.height;
    CGRect stringRect = [self boundingRectWithSize:CGSizeMake(maxWidth, maxHeight)
                                           options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading
                                        attributes:@{NSFontAttributeName :aFont}
                                           context:nil];
    
    return stringRect.size;
}


#pragma mark - private methods
//删除空格换行
- (NSString *)removeWhiteSpaces {
    NSString *trimmedString = [self stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return trimmedString;
}

- (BOOL)containsString:(NSString *)aString withAllString:(NSString *)allString {
    NSRange range = [[allString lowercaseString] rangeOfString:[aString lowercaseString]];
    return range.location != NSNotFound;
}

@end
